package com.example.nbb.Service;

import com.example.nbb.Dao.LooseChangeDao;
import com.example.nbb.Entity.ResultEntity;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class LooseChangeService {
    @Resource
    LooseChangeDao looseChangeDao;

    private static final Logger logger = LoggerFactory.getLogger(LooseChangeService.class);//日志。getLogger需要传入当前的类名

    /**
     * 获取用户零钱余额
     *
     * @param userId 用户id
     * @return 返回
     */
    public String selectByUserId(int userId) {
        try {
            return ResultEntity.success(looseChangeDao.selectByUserId(userId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 减少金额
     *
     * @param userId      用户id
     * @param looseChange 减去的金额
     * @return 返回
     */
    public String decreaseLooseChange(int userId, double looseChange) {
        try {
            return ResultEntity.success(looseChangeDao.decreaseLooseChange(userId, looseChange));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 增加金额
     *
     * @param userId      用户id
     * @param looseChange 加上的金额
     * @return 返回
     */
    public String augmentLooseChange(int userId, double looseChange) {
        try {
            return ResultEntity.success(looseChangeDao.augmentLooseChange(userId, looseChange));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }
}
